package com.example.demo;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.ContentCachingResponseWrapper;

public class AdminInterceptor implements  HandlerInterceptor {

    /**
     * 在请求处理之前进行调用（Controller方法调用之前）
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        /*
        System.out.println("preHandle开始");
        System.out.println("preHandle结束");*/
        return true;//如果设置为false时，被请求时，拦截器执行到此处将不会继续操作
        //如果设置为true时，请求将会继续执行后面的操作
    }

    /**
     * 请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws IOException {
//         System.out.println("执行了TestInterceptor的postHandle方法");
       /* System.out.println("postHandle开始");
        System.out.println("###############response:"+response.getBufferSize());
        ContentCachingResponseWrapper res = new ContentCachingResponseWrapper(response);
        byte[] body = res. getContentAsByteArray();
        System.out.println(readPostJsonData(response));
        System.out.println("postHandle结束");*/
    }

    /**
     * 在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws IOException {
/*
        System.out.println("----------------afterCompletion开始-------------");

        System.out.println("request.getHeaderNames():");
        Enumeration e = request.getHeaderNames();
        while(e.hasMoreElements()){
            Object o = e.nextElement();
            System.out.println((String)o+' '+request.getHeader((String)o));
        }
        System.out.println("request.getParameterNames():");
        Enumeration e2 = request.getParameterNames();
        while(e2.hasMoreElements()){
            Object o = e2.nextElement();
            System.out.println((String)o+' '+request.getParameter((String)o));
        }

        System.out.println("**************afterCompletion结束*************");*/
    }

    public static String readPostJsonData(HttpServletResponse response) throws IOException {

    if (response instanceof ContentCachingResponseWrapper) {

    ContentCachingResponseWrapper responseWrapper = (ContentCachingResponseWrapper) response;

      int contentLenth = ((ContentCachingResponseWrapper) response).getContentSize();

      if (contentLenth <= 0) {

        return null;

      }

      byte[] bytes = new byte[contentLenth];

      InputStream is = responseWrapper.getContentInputStream();

      for (int index = 0; index < contentLenth; index++) {

        int value = is.read();

        if (value == -1) {

//        is.reset();

          break;

        }

        bytes[index] = (byte) value;

      }

      responseWrapper.getContentAsByteArray();//这里是为了测试

      String data = new String(bytes);

      System.out.println("http util .........data:" + data);

      return data;

    }

    System.out.println("http util no data in request body");

    return "";

  }



}